package com.ysd.lis.controller.lab;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ysd.common.Result;
import com.ysd.common.dto.OrganizationDTO;
import com.ysd.lis.annotation.LogDetail;
import com.ysd.lis.aspect.OperationType;
import com.ysd.lis.controller.pub.BaseController;
import com.ysd.lis.entity.PubReport;
import com.ysd.lis.entity.SysUser;
import com.ysd.lis.request.ReportPageParam;
import com.ysd.lis.service.SysBasDictDetailService;
import com.ysd.lis.service.lab.PubReportService;
import com.ysd.util.RedisUserManager;
import com.ysd.util.ToolsUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * SYS_REPORT前端控制器
 * </p>
 *
 * @author Bai
 * @since 2023-11-13
 */
@Api(tags = "报表管理")
@RestController
@RequestMapping("/api/pubReport")
public class PubReportController extends BaseController {

    @Autowired
    PubReportService pubReportService;
    @Autowired
    SysBasDictDetailService sysBasDictDetailService;


    @ApiOperation("分页获取报表信息")
    @GetMapping("/getReportList")
    public Result getReportList(ReportPageParam reportPageParam) {
        return pubReportService.getPageList(reportPageParam);
    }

    @ApiOperation("添加业务表单")
    @PostMapping("/addReportBusiness")
    @LogDetail(requestParam = "addReportBusiness", operationType = OperationType.INSERT, module = "报表管理", content = "通过系统管理模块，使用报表管理新增了：【name】业务表单")
    public Result addReportBusiness(@RequestBody PubReport pubReport) {
        return pubReportService.addReportBusiness(pubReport);
    }

    @ApiOperation("另存为业务表单")
    @PostMapping("/saveAsReportBusiness")
    @LogDetail(businessName = "reportName", requestParam = "saveAsReportBusiness", operationType = OperationType.INSERT, module = "报表管理", content = "通过系统管理模块，使用报表管理将【saveAsName】另存为：【name】业务表单")
    public Result saveAsReportBusiness(@RequestBody PubReport pubReport) {
        return pubReportService.saveAsReportBusiness(pubReport);
    }

    @ApiOperation("删除业务表单")
    @PostMapping("/deleteReport")
    @LogDetail(requestParam = "deleteReport", operationType = OperationType.DELETE, module = "报表管理", content = "通过系统管理模块，使用报表管理将【name】模版删除")
    public Result deleteReport(@RequestBody PubReport pubReport) {
        return pubReportService.deleteReport(pubReport);
    }

    @ApiOperation("修改业务表单")
    @PostMapping("/updateReportBusiness")
    @LogDetail(businessName = "reportName", requestParam = "updateReportBusiness", operationType = OperationType.UPDATE, module = "报表管理", content = "通过系统管理模块，使用报表管理，修改了【saveAsName】业务表单")
    public Result updateReportBusiness(@RequestBody PubReport pubReport) {
        if (ToolsUtils.isEmpty(pubReport)) {
            return Result.fail("修改内容入参不可为空");
        }
        pubReportService.updateById(pubReport);
        return Result.succ("修改成功");
    }

    @ApiOperation("设置常用业务表单")
    @PostMapping("/updateUse")
    @LogDetail(businessName = "name", requestParam = "updateUse", operationType = OperationType.UPDATE, module = "报表管理", content = "通过系统管理模块，使用报表管理将【name】模版设置成选用状态")
    public Result updateUse(@RequestBody PubReport pubReport) {
        return pubReportService.updateUse(pubReport);
    }

    @ApiOperation("设置是否含图业务表单")
    @PostMapping("/setIncludeImages")
    @LogDetail(businessName = "name", requestParam = "setIncludeImages", operationType = OperationType.UPDATE, module = "报表管理", content = "通过系统管理模块，使用报表管理将【name】模版设置是否包含图片")
    public Result setIncludeImages(@RequestBody PubReport pubReport) {
        return pubReportService.setIncludeImages(pubReport);
    }

    @ApiOperation("根据主键获取报表模版")
    @PostMapping("/getReportById")
    public Result getReportById(@RequestBody PubReport pubReport) {
        return pubReportService.getReportById(pubReport);
    }


    @ApiOperation("批量上传")
    @PostMapping("/saveBatch")
    @LogDetail(businessName = "reportName", requestParam = "saveBatch", operationType = OperationType.UPDATE, module = "报表管理", content = "通过系统管理模块，使用批量存储功能，向【reportName】类型的报表中，新增了【name】基础模版")
    public Result saveBatch(@RequestBody PubReport pubReport) {
        return pubReportService.saveBatch(pubReport);
    }

    @ApiOperation("根据不同条件获取相应的报表模版")
    @GetMapping("/getReportByAllReq")
    public Result getReportByAllReq(@RequestParam String menuId) {
        PubReport pubReport = new PubReport();
        pubReport.setMenuId(menuId);
        return pubReportService.getReportByAllReq(pubReport);
    }

    @ApiOperation("从词典中获取模块与业务表单树形结构")
    @PostMapping("/getModuleAndDictFormList")
    public Result getModuleAndDictFormList() {
        return pubReportService.getModuleAndDictFormList();
    }

    @ApiOperation("从词典中获取表单模块列表")
    @PostMapping("/getModuleDictList")
    public Result getModuleDictList() {
        return pubReportService.getModuleDictList();
    }

    @ApiOperation("从词典中获取表单列表")
    @PostMapping("/getFormDictList")
    public Result getFormDictList() {
        return pubReportService.getFormDictList();
    }


    @ApiOperation("根据报告组别ID获取报告列表")
    @GetMapping("/getReportByCommonId")
    public Result getReportByCommonId(@RequestParam String commonId) {
        return pubReportService.getReportByCommonId(commonId);
    }

    @ApiOperation("根据报表编码获取报表ID")
    @GetMapping("/getReportByReportCode")
    public Result getReportByReportCode(@RequestParam String reportCode) {
        return pubReportService.getReportByReportCode(reportCode);
    }

    @ApiOperation("根据报表code获取报表信息")
    @GetMapping("/getReportByCode")
    public Result getReportByCode(@RequestParam String code) {
        SysUser sysUser = RedisUserManager.getUser();
        LambdaQueryWrapper<PubReport> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PubReport::getCode, code)
                .eq(PubReport::getOrgId, sysUser.getOrgId())
                .eq(PubReport::getDelFlag, 0)
                .eq(PubReport::getIsUse,1);
        PubReport one = pubReportService.getOne(lambdaQueryWrapper);
        return Result.succ(1,"查询成功",one);
    }

    @ApiOperation("根据Url获取报表ID")
    @GetMapping("/getReportIdByUrl")
    public Result getReportIdByUrl(@RequestParam String url) {
        return pubReportService.getReportIdByUrl(url);
    }

    @ApiOperation("区域lis")
    @PostMapping("/copyBaseData")
    public Result copyBaseData(@RequestBody OrganizationDTO organizationDTO) {
        return pubReportService.copyBaseData(organizationDTO);
    }

    //根据业务表单字典YWBD(固定值) 传业务表单code取出common_id
    @ApiOperation("根据业务表单字典YWBD(固定值) 传业务表单code取出报表ID")
    @GetMapping("/getReportByDictDetailVaule")
    public Result getReportByDictDetailVaule(@RequestParam String detailVaule) {
        return pubReportService.getReportByDictDetailVaule(detailVaule);
    }

    @ApiOperation("根据业务表单类型编号获取相应的启用模板")
    @GetMapping("/getReportByBusFormCode")
    public Result getPubReportByDictDetailVaule(@RequestParam String code) {
        PubReport rpt = pubReportService.getPubReportByDictDetailVaule(code);
        return Result.succ(1, "成功", rpt);
    }

    @ApiOperation("获取鉴定结果模板")
    @GetMapping("/getPyReport")
    public Result getPyReport(String sampleda, String sampleNo,String rptGroup) {
        return pubReportService.getPyReport(sampleda, sampleNo,rptGroup);
    }

    @ApiOperation("微生物签收:重打条码")
    @GetMapping("/againReqNo")
    public Result againReqNo() {
        return pubReportService.againReqNo();
    }
    @ApiOperation("微生物签收:重打标签")
    @GetMapping("/againLable")
    public Result againLable() {
        return pubReportService.againLable();
    }
    @ApiOperation("微生物签收:打印")
    @GetMapping("/print")
    public Result print() {
        return pubReportService.print();
    }
}
